<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-descriptions direction="vertical" :column="2" border>
            <el-descriptions-item label="申请日期">{{ ruleForm.created_at }}</el-descriptions-item>
            <el-descriptions-item label="申请付款金额">{{ ruleForm.price }}</el-descriptions-item>
            <el-descriptions-item label="付款原因说明">{{ ruleForm.reason }}</el-descriptions-item>
            <el-descriptions-item label="付款方式">{{ ruleForm.payment_type }}</el-descriptions-item>
            <el-descriptions-item label="收款方信息">{{ ruleForm.pay_info }}</el-descriptions-item>
        </el-descriptions>
        <el-form-item label="付款日期" prop="pay_date">
            <el-date-picker v-model="ruleForm.pay_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="发票类型" prop="invoice_type">
            <el-radio-group v-model="ruleForm.invoice_type">
                <el-radio value="‌增值税专用发票‌" >‌增值税专用发票‌</el-radio>
                <el-radio value="普通发票‌" >普通发票‌</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="发票编号" prop="invoice_numbers">
            <el-input v-model="ruleForm.invoice_numbers" />
        </el-form-item>
        <el-form-item label="发票" prop="invoice_file">
            <component :is="uploadImg" :data="uploadData" @saveUpload="saveUpload" />
        </el-form-item>
        <el-form-item label="操作" prop="status">
            <el-radio-group v-model="ruleForm.status">
                <el-radio value="approved" >通过</el-radio>
                <el-radio value="rejected" >驳回</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="review_comment">
            <el-input v-model="ruleForm.review_comment" type="textarea" :autosize="{ minRows: 4 }" />
        </el-form-item>
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import uploadImg from '@/components/uploadImg.vue'
import http from '../util/request';
const loading = ref<boolean>(false)
let uploadData:any = ref({prefix:'apply/',type:'',image:''})
interface RuleForm {
    id: number
    reviewer_id:number
    status: string
    review_comment:string
    created_at:string
    price:number
    reason:string
    payment_type:string
    pay_info:string
    pay_date:string
    invoice_numbers:string
    invoice_file:string
    invoice_type:string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    reviewer_id:0,
    status: '',
    review_comment:'/',
    created_at:'',
    price:0,
    reason:'/',
    payment_type:'/',
    pay_info:'/',
    pay_date:'',
    invoice_numbers:'/',
    invoice_file:'',
    invoice_type:'',
})

const onGetData = () => {
    http.get('/approve/'+ruleForm.value.id).then((response:any)=>{
        ruleForm.value = response.data.data
        uploadData.value.image = ruleForm.value.invoice_file
    })
}

const props = defineProps<{
    data:any
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    ruleForm.value.reviewer_id = 0
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    status: [
        { required: true, message: '请选择操作类型', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let obj:any = {
                application_id:ruleForm.value.id,
                comment:ruleForm.value.review_comment,
                status:ruleForm.value.status,
                invoice_numbers:ruleForm.value.invoice_numbers,
                invoice_file:ruleForm.value.invoice_file,
                invoice_type:ruleForm.value.invoice_type,
                pay_date:ruleForm.value.pay_date
            }
            http.patch('/approve/review',obj).then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}


const saveUpload = (data:string) => {
    ruleForm.value.invoice_file = data
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])
</script>
<style scoped>
</style>
